import { getToken, removeToken, setToken } from '@/utils/auth'
import { getInfo, getUserDetailById } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: getToken() || null,
    userInfo: {}
  },
  mutations: {
    // 更新token
    updataToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 删除token
    delToken(state) {
      state.token = ''
      removeToken()
    },
    // 设置用户信息
    updataUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // 删除用户信息
    delUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getUserInfo(context) {
      try {
        const res = await getInfo()
        // console.log('getUserInfo', res)
        const info = await getUserDetailById(res.data.userId)
        // console.log('info', info)
        context.commit('updataUserInfo', { ...res.data, ...info.data })
      } catch (error) {
        console.log(error)
      }
    }
  }
}
